<template>
	<view>
		<view class="status-section">
			<image :src="orderStatus.image" class="icon" />
			<text class="label-text">{{orderStatus.text}}</text>
		</view>
		<!-- 地址 -->
		<view class="address-section">
			<view class="order-content">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{order.receiverName}}</text>
						<text class="mobile">{{order.receiverPhone}}</text>
					</view>
					<text class="address">{{order.receiverProvince}} {{order.receiverCity}} {{order.receiverRegion}}
						{{order.receiverDetailAddress}}</text>
				</view>
			</view>

			<image class="a-bg" src=""></image>
		</view>

		<view class="goods-section">
			<view class="g-header b-b">
				<text class="name">商品信息</text>
			</view>
			<!-- 商品列表 -->
			<view class="g-item" v-for="item in order.orderItemList" :key="item.id">
				<image :src="item.productPic"></image>
				<view class="right">
					<text class="title clamp">{{item.productName}}</text>
					<text class="spec">{{item.productAttr | formatProductAttr}}</text>
					<text class="promotion clamp">{{item.promotionName}}</text>
					<view class="price-box">
						<text class="price">￥{{item.productPrice}}</text>
						<text class="number">x {{item.productQuantity}}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品合计</text>
				<text class="cell-tip">￥{{order.totalAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">￥{{order.freightAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">活动优惠</text>
				<text class="cell-tip red">-￥{{order.promotionAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">优惠券</text>
				<text class="cell-tip red">-￥{{order.couponAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">积分抵扣</text>
				<text class="cell-tip red">-￥{{order.integrationAmount}}</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<text class="cell-tip">{{order.note}}</text>
			</view>
		</view>

		<!-- 订单明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">订单编号</text>
				<text class="cell-tip">{{order.orderSn}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">提交时间</text>
				<text class="cell-tip">{{order.createTime | formatDateTime}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">支付方式</text>
				<text class="cell-tip">{{order.payType | formatPayType}}</text>
			</view>
			<view class="yt-list-cell b-b" v-if="order.status==1||order.status==2||order.status==3">
				<text class="cell-tit clamp">实付金额</text>
				<text class="cell-tip">￥{{order.payAmount}}</text>
			</view>
			<view class="yt-list-cell b-b" v-if="order.status==1||order.status==2||order.status==3">
				<text class="cell-tit clamp">付款时间</text>
				<text class="cell-tip">{{order.paymentTime | formatDateTime}}</text>
			</view>
		</view>

		<!-- 商品评价 -->
		<view class="comments-section" v-if="commentList.length > 0">
			<view class="section-header b-b">
				<text class="title">商品评价</text>
				<text class="more" @click="viewMoreComments">查看更多</text>
			</view>
			<view class="comment-item" v-for="(comment, index) in commentList" :key="index">
				<view class="user-info">
					<image class="avatar" :src="comment.memberIcon || '/static/img/default-avatar.png'" mode="aspectFill"></image>
					<text class="nickname">{{comment.memberNickName || '匿名用户'}}</text>
					<view class="star">
						<text class="yticon icon-shitinghuiyuan" 
							v-for="star in 5" 
							:key="star" 
							:class="{'active': star <= comment.star}">
						</text>
					</view>
				</view>
				<view class="content">{{comment.content}}</view>
				<!-- 评价图片 -->
				<view class="image-list" v-if="comment.pics">
					<image 
						v-for="(pic, picIndex) in formatPics(comment.pics)" 
						:key="picIndex" 
						:src="pic" 
						class="comment-img" 
						mode="aspectFill"
						@click="previewImage(formatPics(comment.pics), picIndex)">
					</image>
				</view>
				<view class="attr">{{comment.productAttribute}}</view>
				<view class="time">{{comment.createTime | formatDateTime}}</view>
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer" v-if="order.status==0||order.status==2||order.status==3">
			<view class="action-box b-t" v-if="order.status==0">
				<button class="action-btn" @click="cancelOrder(order.id)">取消订单</button>
				<button class="action-btn recom" @click="payOrder(order.id)">立即付款</button>
			</view>
			<view class="action-box b-t" v-if="order.status == 2">
				<button class="action-btn">查看物流</button>
				<button class="action-btn recom" @click="receiveOrder(order.id)">确认收货</button>
			</view>
			<view class="action-box b-t" v-if="order.status == 3">
				<button class="action-btn">申请售后</button>
				<button class="action-btn recom" v-if="order.commentTime === null" @click="evaluateOrder">评价商品</button>
				<button class="action-btn recom" v-else @click="viewCommentDetail">查看评价</button>
			</view>
			<view class="price-content" v-if="order.status==0">
				<text>应付金额</text>
				<text class="price-tip">￥</text>
				<text class="price">{{order.payAmount}}</text>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		fetchOrderDetail,
		cancelUserOrder,
		confirmReceiveOrder
	} from '@/api/order.js';
	import {
		formatDate
	} from '@/utils/date';
	import {
		listComments
	} from '@/api/comment.js';
	export default {
		data() {
			return {
				orderId: null,
				order: {},
				orderStatus: {},
				commentList: []
			}
		},
		onLoad(option) {
			//商品数据
			this.orderId = option.orderId;
			this.loadData();
		},
		filters: {
			formatProductAttr(jsonAttr) {
				let attrArr = JSON.parse(jsonAttr);
				let attrStr = '';
				for (let attr of attrArr) {
					attrStr += attr.key;
					attrStr += ":";
					attrStr += attr.value;
					attrStr += ";";
				}
				return attrStr
			},
			formatDateTime(time) {
				if (time == null || time === '') {
					return 'N/A';
				}
				let date = new Date(time);
				return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
			},
			formatPayType(payType) {
				if (payType == 0) {
					return "未支付";
				} else if (payType == 1) {
					return "支付宝支付";
				} else if (payType == 2) {
					return "微信支付";
				}
				return null;
			},
		},
		methods: {
			//生成确认单信息
			async loadData() {
				fetchOrderDetail(this.orderId).then(response => {
					this.order = response.data;
					this.setOrderStatus(this.order.status);
					// 如果订单完成，尝试加载评论
					if (this.order.status === 3 && this.order.orderItemList && this.order.orderItemList.length > 0) {
						this.loadComments();
					}
				});
			},
			// 加载商品评论
			loadComments() {
				// 使用第一个商品ID加载评论
				const firstItem = this.order.orderItemList[0];
				if (firstItem && firstItem.productId) {
					listComments(firstItem.productId, 3, 1).then(response => {
						if (response.code === 200 && response.data) {
							this.commentList = response.data.list || [];
						}
					}).catch(error => {
						console.error('获取评论失败:', error);
					});
				}
			},
			// 格式化图片地址
			formatPics(pics) {
				if (!pics) return [];
				if (typeof pics === 'string') {
					return pics.split(',').filter(pic => pic && pic.trim() !== '');
				}
				return Array.isArray(pics) ? pics : [];
			},
			// 预览图片
			previewImage(pics, current) {
				if (!pics || !pics.length) return;
				uni.previewImage({
					urls: pics,
					current: pics[current]
				});
			},
			// 查看更多评论
			viewMoreComments() {
				if (this.order.orderItemList && this.order.orderItemList.length > 0) {
					const firstItem = this.order.orderItemList[0];
					uni.navigateTo({
						url: `/pages/product/product?id=${firstItem.productId}`
					});
				}
			},
			submit() {},
			stopPrevent() {},
			//取消订单
			cancelOrder(orderId) {
				let superThis = this;
				uni.showModal({
					title: '提示',
					content: '是否要取消该订单？',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '请稍后'
							})
							cancelUserOrder({
								orderId: orderId
							}).then(response => {
								uni.hideLoading();
								superThis.loadData();
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//支付订单
			payOrder(orderId) {
				uni.redirectTo({
					url: `/pages/money/pay?orderId=${orderId}`
				});
			},
			//确认收货
			receiveOrder(orderId) {
				let superThis = this;
				uni.showModal({
					title: '提示',
					content: '是否要确认收货？',
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '请稍后'
							})
							confirmReceiveOrder({
								orderId: orderId
							}).then(response => {
								uni.hideLoading();
								superThis.loadData();
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//评价商品
			evaluateOrder() {
				// 如果有订单项
				if(this.order.orderItemList && this.order.orderItemList.length > 0) {
					// 这里简单处理，直接评价第一个商品
					const orderItem = this.order.orderItemList[0];
					uni.setStorageSync('currentOrderItem', JSON.stringify(orderItem));
					
					uni.navigateTo({
						url: `/pages/order/evaluate?orderId=${this.order.id}&orderItemId=${orderItem.id}`
					});
				} else {
					uni.showToast({
						title: '没有可评价的商品',
						icon: 'none'
					});
				}
			},
			// 查看评价详情
			viewCommentDetail() {
				if(this.order.orderItemList && this.order.orderItemList.length > 0) {
					// 这里修改为跳转到商品详情页
					const orderItem = this.order.orderItemList[0];
					uni.navigateTo({
						url: `/pages/product/product?id=${orderItem.productId}`
					});
				} else {
					uni.showToast({
						title: '没有评价信息',
						icon: 'none'
					});
				}
			},
			//设置状态信息
			setOrderStatus(status) {
				//订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
				let statusTip;
				let image;
				if (status == 0) {
					statusTip = '待付款';
					image = '/static/newimg/paying.png';
				} else if (status == 1) {
					statusTip = '待发货';
					image = '/static/newimg/shipping.png';
				} else if (status == 2) {
					statusTip = '待收货';
					image = '/static/newimg/receiving.png';
				} else if (status == 3) {
					statusTip = '已完成';
					image = '/static/newimg/received.png';
				} else if (status == 4) {
					statusTip = '已关闭';
					image = '/static/newimg/cancelled.png';
				} else if (status == 5) {
					statusTip = '无效订单';
					image = '/static/newimg/cancelled.png';
				}
				this.orderStatus = {
					text: statusTip,
					image: image
				};
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	.status-section {
		position: relative;
		width: 100%;
		height: 180upx;
		background: #fa436a;
		padding-bottom: 40upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		
		.icon {
			width: 80upx;
			height: 80upx;
		}
		
		.label-text {
			margin-top: 10upx;
			font-size: 28upx;
			color: #fff;
		}
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: #303133;
		}

		.name {
			font-size: 34upx;
			margin-right: 10upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			font-size: 30upx;
			color: #909399;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 6upx;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}
			
			.promotion {
				font-size: 26upx;
				color: $base-color;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}
				.number{
					margin-left: 4upx;
					font-size: 26upx;
					color: $font-color-base;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.red{
				color: $base-color;
			}
			&.blue{
				color: #fa436a;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}
		.icon-alipay {
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}

		.tit {
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30upx;
			display: flex;
			align-items: baseline;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}

	/* 评价 */
	.comments-section {
		margin-top: 16upx;
		background: #fff;
		padding: 0 30upx;

		.section-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 84upx;
			position: relative;
			padding-bottom: 10upx;

			.title {
				font-size: 30upx;
				color: $font-color-dark;
				font-weight: bold;
			}

			.more {
				font-size: 26upx;
				color: $font-color-light;
			}
		}

		.comment-item {
			margin-top: 20upx;
			padding-bottom: 20upx;
			border-bottom: 1px solid #f6f6f6;

			&:last-child {
				border-bottom: none;
			}

			.user-info {
				display: flex;
				align-items: center;
				margin-bottom: 10upx;

				.avatar {
					width: 60upx;
					height: 60upx;
					border-radius: 50%;
					margin-right: 20upx;
				}

				.nickname {
					font-size: 26upx;
					color: $font-color-dark;
					margin-right: 20upx;
				}

				.star {
					display: flex;
					align-items: center;

					.yticon {
						font-size: 24upx;
						color: #ccc;
						margin-right: 5upx;

						&.active {
							color: #ffb400;
						}
					}
				}
			}

			.content {
				font-size: 28upx;
				color: $font-color-dark;
				line-height: 1.6;
			}

			.image-list {
				display: flex;
				flex-wrap: wrap;
				margin-top: 10upx;

				.comment-img {
					width: 150upx;
					height: 150upx;
					margin-right: 10upx;
					margin-bottom: 10upx;
					border-radius: 6upx;
				}
			}

			.attr {
				font-size: 24upx;
				color: $font-color-light;
				margin-top: 10upx;
			}

			.time {
				font-size: 24upx;
				color: $font-color-light;
				margin-top: 10upx;
			}
		}
	}

	.action-box {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		height: 100%;
		position: relative;
		padding-right: 30upx;
	}

	.action-btn {
		width: 160upx;
		height: 60upx;
		margin: 0;
		margin-left: 24upx;
		padding: 0;
		text-align: center;
		line-height: 60upx;
		font-size: 26upx;
		color: $font-color-dark;
		background: #fff;
		border-radius: 100px;
		border:1px solid #ccc;
	}
	.action-btn:after {
		border-radius: 100px;
	}
	.action-btn.recom {
		color: #fff;
		background: $base-color;
		border:1px solid $base-color;
	}
</style>

